<template>
  <div>
    <BasicTable
      @register="registerTable"
      :can-resize="true"
      :bordered="true"
      :striped="false"
      :loading="false"
    >
      <template #toolbar>
        <a-button type="success" size="small" @click="onAddButtonClick()"
          ><PlusOutlined /> 新增
        </a-button>
      </template>

      <template #action="{ record }">
        <TableAction
          :actions="[
            {
              icon: 'clarity:note-edit-line',
              label: '编辑',
              onClick: onEditButtonClick.bind(null, record),
            },
            {
              icon: 'ant-design:delete-outlined',
              label: '删除',
              color: 'error',
              popConfirm: {
                title: '是否确认删除',
                confirm: onDeleteButtonClick.bind(null, record),
              },
            },
            {
              icon: 'clarity:note-edit-line',
              label: 'SQL管理',
              onClick: onMenageSQLButtonClick.bind(null, record),
            },
            {
              icon: 'clarity:note-edit-line',
              label: '列管理',
              onClick: onMenageColumnButtonClick.bind(null, record),
            },
          ]"
        />
      </template>
    </BasicTable>
    <EditDBTbleFormModal @register="registerEditFormModal" @success="reload" />
    <DBTableSQLIndex @register="registerSQLIndex" />
    <DBTableColumnIndex @register="registerColumnIndex" />
  </div>
</template>
<script lang="ts">
  import { defineComponent } from 'vue';
  import { BasicTable, useTable, TableAction } from '/@/components/Table';

  import { QcUnifyTransToChildProps } from '/#/QcUnify';
  import { OperationTypeEnum } from '/@/enums/appEnum';
  import { useMessage } from '/@/hooks/web/useMessage';

  import { dbTableColumns, dbTableQueryConditionFormSchema } from './dbtable.data';
  import { queryDBTable, deleteDBTable } from '/@/api/platform/dbTable';

  import EditDBTbleFormModal from './EditDBTableModal.vue';
  import DBTableSQLIndex from './dbtablesql/DBTableSQLIndex.vue';
  import DBTableColumnIndex from './dbtablecolumn/DBTableColumnIndex.vue';

  import { useModal } from '/@/components/Modal';
  import { PlusOutlined } from '@ant-design/icons-vue';
  import { useDrawer } from '/@/components/Drawer';

  export default defineComponent({
    name: 'DBTableManegement',
    components: {
      BasicTable,
      TableAction,
      PlusOutlined,
      EditDBTbleFormModal,
      DBTableSQLIndex,
      DBTableColumnIndex,
    },
    setup() {
      const { createMessage } = useMessage();
      const [registerEditFormModal, { openModal: openEditFormModal }] = useModal();
      const [registerSQLIndex, { openDrawer: openSQLIndxDrawer }] = useDrawer();
      const [registerColumnIndex, { openDrawer: openColumnIndxDrawer }] = useDrawer();

      const [registerTable, { reload }] = useTable({
        title: '数据库表管理',
        api: queryDBTable,
        columns: dbTableColumns,
        formConfig: {
          labelWidth: 100, //设置查询表单中label的宽度
          schemas: dbTableQueryConditionFormSchema, // form查询表单
          showResetButton: true, //查询条件部分中是否显示重置按钮
        },
        useSearchForm: true,
        showTableSetting: true,
        bordered: true,
        size: 'small',
        actionColumn: {
          width: 300,
          title: '操作',
          dataIndex: 'action',
          slots: { customRender: 'action' },
          fixed: 'right', //表格操作列是否固定，不设置时默认固定在右侧
        },
      });

      /**
       * 新增按钮事件，打开新增表单
       */
      function onAddButtonClick() {
        //构造父组件给子组件传值对象
        let propsData: QcUnifyTransToChildProps = {
          moduleName: '数据库表管理',
          operateType: OperationTypeEnum.ADD,
        };
        openEditFormModal(true, propsData);
      }

      /**
       * 编辑按钮事件，打开编辑表单
       */
      function onEditButtonClick(record: Recordable) {
        //构造父组件给子组件传值对象
        let propsData: QcUnifyTransToChildProps = {
          moduleName: '数据库表管理',
          operateType: OperationTypeEnum.EDIT,
          dataId: record.code,
        };
        openEditFormModal(true, propsData);
      }

      /**
       * 删除按钮事件
       */
      function onDeleteButtonClick(record: Recordable) {
        deleteDBTable(record.code)
          .then((res) => {
            // console.log('res', res);
            //res判断结果：同时==undefined和null
            if (res == undefined && (res == null || Object.keys(res).length == 0)) {
              createMessage.success('删除成功。', 2);
              reload();
            }
          })
          .catch((ex) => {
            console.log('catch ', ex);
            createMessage.error(ex.msg);
          });
      }

      /**
       * 数据库表sql管理按钮事件，打开指定表sql管理界面
       */
      function onMenageSQLButtonClick(record: Recordable) {
        //构造父组件给子组件传值对象
        let propsData: QcUnifyTransToChildProps = {
          moduleName: record.name,
          operateType: OperationTypeEnum.CUSTOME,
          operateString: 'SQL管理',
          dataId: record.code,
        };
        openSQLIndxDrawer(true, propsData);
      }

      /**
       * 数据库表列管理按钮事件，打开指定表的列管理界面
       */
      function onMenageColumnButtonClick(record: Recordable) {
        //构造父组件给子组件传值对象
        let propsData: QcUnifyTransToChildProps = {
          moduleName: record.name,
          operateType: OperationTypeEnum.CUSTOME,
          operateString: '列管理',
          dataId: record.code,
        };
        openColumnIndxDrawer(true, propsData);
      }

      return {
        registerTable,
        registerEditFormModal,
        onAddButtonClick,
        onEditButtonClick,
        onDeleteButtonClick,
        onMenageSQLButtonClick,
        onMenageColumnButtonClick,
        registerSQLIndex,
        registerColumnIndex,
        reload,
      };
    },
  });
</script>
